/*
 * Copyright (C) 2011 by Fabien Barbero
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flickr.api.photos.upload;

import com.flickr.api.AbstractFlickrService;
import com.flickr.api.ApiKey;
import com.flickr.api.CommandArguments;
import com.flickr.api.FlickrServiceException;
import com.flickr.api.auth.AuthenticationService;
import com.flickr.api.photos.Photo;
import com.google.inject.Inject;
import com.sun.jersey.multipart.MultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.ws.rs.core.MediaType;

/**
 *
 * @author Fabien Barbero
 */
public class PhotosUploadServiceImpl extends AbstractFlickrService implements PhotosUploadService {

    private static final String UPLOAD_URL = "http://api.flickr.com/services/upload/";
    private final ApiKey apiKey;
    private final AuthenticationService authService;

    @Inject
    public PhotosUploadServiceImpl(ApiKey apiKey, AuthenticationService authService) {
        this.apiKey = apiKey;
        this.authService = authService;
    }

    @Override
    public Photo uploadPhoto(File file) throws FlickrServiceException, IOException {
        if (!file.exists()) {
            throw new FileNotFoundException("File doesn't exists " + file);
        }

        CommandArguments args = new CommandArguments(apiKey);

        MultiPart form = args.toSignedMultipart();
        form.getBodyParts().add(new FileDataBodyPart("photo", file));
        
        UploadedPhotoResponse photo = getClient().resource(UPLOAD_URL).type(MediaType.MULTIPART_FORM_DATA).post(UploadedPhotoResponse.class, form);
        checkResponse(photo, args);
        return Photo.create(photo.getPhotoId());
    }

    @Override
    public Photo uploadPhoto(File file, String title, String description) throws FlickrServiceException, IOException {
        if (!file.exists()) {
            throw new FileNotFoundException("File doesn't exists " + file);
        }

        CommandArguments args = new CommandArguments(apiKey);
        args.put("title", title);
        args.put("description", description);

        MultiPart form = args.toSignedMultipart();
        form.getBodyParts().add(new FileDataBodyPart("photo", file));
        
        UploadedPhotoResponse photo = getClient().resource(UPLOAD_URL).type(MediaType.MULTIPART_FORM_DATA).post(UploadedPhotoResponse.class, form);
        checkResponse(photo, args);
        return Photo.create(photo.getPhotoId());
    }
}
